<?php
/**
 * @auth tengjianxing
 * */

//note 接口合法性验证
define('INTERFACE_CODE', md5('is-reviews-nene'));
if( $_GET['code'] != INTERFACE_CODE ) exit('error code!');

define('IN_WONDERSHARE', true);
define('CACHE_DIR', '../../image2/cache/');//缓存文件夹

require_once '../../conf/conf.php';
require_once '../../includes/cls_mysql.php';
require_once 'function.php';

$db = new cls_mysql(DB_HOST, DB_USER, DB_PASS, DB_DATABASE);

$tem_product = array(
					'product' => array('table' => 'Table_137', 'id' => 'Field_21669'),
					'product_single' => array('table' => 'Table_147', 'id' => 'Field_21726'),
					'product_review' => array('table' => 'Table_193', 'id' => 'Field_22043')
				);
				
//note 支持与反对
function s_o( $db ){
	$did = post('did');
	$type = post('type');
	$ip = post('ip');
	
	$sql = "SELECT COUNT(*) num FROM product_reviews_ip WHERE did = {$did} AND ip = '{$ip}'";
	$num = $db->getOne( $sql );
	
	//有支持或反对过
	if( $num ){
		echo 0;
		exit;
	}
	
	$sql = "INSERT INTO product_reviews_ip SET did = {$did}, ip = '{$ip}'";
	$db->query( $sql );
	
	$so = $type == 1 ? 'support' : 'oppose';
	
	$sql = "UPDATE product_reviews_detail SET {$so} = {$so} + 1 WHERE did = {$did}";
	$ret = $db->query( $sql );
	echo $ret ? 1 : 0;
}

//note 添加评论
function add( $db ){
	$pid = post('pid');
	
	$sql = "INSERT INTO product_reviews_detail(pid,web_type,star,title,review,username,email,ip) 
			VALUE({$pid },{$_POST['web_type']},{$_POST['star']},'{$_POST['title']}','{$_POST['review']}','{$_POST['username']}','{$_POST['email']}','{$_POST['ip']}')";
	$ret = $db->query( $sql );
	
	echo 1;
}

//note 取得产品信息
function get_info( $db, $tem_product ){
	
	$pid = (int) post('pid');
	$web_type = post('web_type');
	
	$data = array();
	
	switch( $web_type ){
		
		case '8'://ai
			$sql = "SELECT p.product_box_middle, t.url, t_s.url AS s_url FROM product AS p
					LEFT JOIN won_product AS won_p ON p.product_id = won_p.won_product_id 
					LEFT JOIN {$tem_product['product']['table']} AS t ON won_p.id = t.{$tem_product['product']['id']}
					LEFT JOIN {$tem_product['product_single']['table']} AS t_s ON won_p.id = t_s.{$tem_product['product_single']['id']}
					WHERE won_p.id = {$pid} AND won_p.won_product_delete = 0";
			$data = $db->getOneRs( $sql );
			$data['url'] = !empty($data['url']) ? $data['url'] : $data['s_url'];
			
			$data['url'] = getReplaceUrl($data['url']);
			$data['store_url'] = 'http://www.iskysoft.com/store.html';
		break;
	
	}

	echo json_encode( $data );
}

//note 取得有评论的产品名
function get_product_had_review( $db, $tem_product ) {
	
	$web_type = get('web_type');

	$sql = "SELECT wp.id, p.product_name FROM product_reviews AS pr
			LEFT JOIN won_product AS wp ON wp.id = pr.pid
			LEFT JOIN product AS p ON wp.won_product_id = p.product_id
			WHERE wp.web_type = '{$web_type}' AND wp.won_product_delete = 0";
	$data = $db->getAll( $sql );
	
	$arr = array();
	foreach( $data as $v ){
		$arr[ $v['id'] ] = $v['product_name'];
	}
	
	echo json_encode( $arr );
}

//note 根据产品名搜索产品评论页面地址
function search( $db , $tem_product){
	
	$web_type = get('web_type');
	$name = get('name');
	
	$sql = "SELECT   won_p.id, t.url
			FROM  won_product won_p 
			LEFT JOIN product p ON won_p.won_product_id = p.product_id
			LEFT JOIN {$tem_product['product_review']['table']} AS t ON won_p.id = t.{$tem_product['product_review']['id']}
			WHERE p.product_name LIKE   '%{$name}%' AND t.url != '' LIMIT 1 ";

	$row = $db->getOneRs( $sql );

	echo getReplaceUrl($row['url']);;
}


//note 取得产品六个产品的评论相关信息
function get_review_info( $db, $tem_product ){
	
	$web_type = get('web_type');
	
	$tmp = CACHE_DIR.'review__get_review_info__'.$web_type.'.php';
	
	//读缓存
	if( file_exists( $tmp )){
		echo file_get_contents( $tmp );
		exit;
	}
	
	$sql = "SELECT r.*, p.product_name, p.product_box_middle, t.url, t_s.url AS s_url, t_r.url AS r_url
			FROM product_reviews r
			LEFT JOIN won_product won_p ON won_p.id = r.pid
			LEFT JOIN product p ON won_p.won_product_id = p.product_id
			LEFT JOIN {$tem_product['product']['table']} AS t ON won_p.id = t.{$tem_product['product']['id']}
			LEFT JOIN {$tem_product['product_review']['table']} AS t_r ON won_p.id = t_r.{$tem_product['product_review']['id']}
			LEFT JOIN {$tem_product['product_single']['table']} AS t_s ON won_p.id = t_s.{$tem_product['product_single']['id']}
			WHERE r.pid IN(150,147,154,148,142,141)";
	
	$data = $db->getAll( $sql );
	
	$arr = array();
	foreach( $data as $k => $v ){
		
		$v['url'] = !empty($v['url']) ? $v['url'] : $v['s_url'];
		$arr[$k]['url'] = getReplaceUrl($v['url']);
		$arr[$k]['r_url'] = getReplaceUrl($v['r_url']);
		$arr[$k]['product_name'] = str_replace('iSkysoft', '', $v['product_name']);
		$arr[$k]['product_box_middle'] = $v['product_box_middle'];
		$arr[$k]['all_num'] = $v['star_1'] + $v['star_2'] + $v['star_3'] + $v['star_4'] + $v['star_5'];
		$arr[$k]['avg'] = sprintf('%0.2f', ($v['star_1'] + $v['star_2'] * 2 + $v['star_3'] * 3 + $v['star_4'] * 4 + $v['star_5'] * 5) / $arr[$k]['all_num']);
		
		$sql = "SELECT username, review, date 
				FROM product_reviews_detail 
				WHERE pid = '{$v['pid']}' AND check_state = 1 AND web_type = '{$web_type}' 
				ORDER BY did DESC 
				LIMIT 1";
		$ret = $db->getOneRs( $sql );
		$arr[$k]['username'] = $ret['username'];
		$arr[$k]['review'] = stripslashes(strlen($ret['review']) > 130 ? substr( $ret['review'], 0, 130 ).'...' : $ret['review']);
		$arr[$k]['date'] = $ret['date'];
	}
	$ret = json_encode( $arr );
	//写缓存
	file_put_contents( $tmp, $ret );
	echo $ret;
}

//note 取得分类评论
function get_all_cat_review( $db, $tem_product ){
	
	$cat_id = get('cat_id');
	$web_type = get('web_type');
	
	$tmp = CACHE_DIR.'review__get_all_cat_review__'.$web_type.'__'.$cat_id.'.php';
	//读缓存
	if( file_exists( $tmp )){
		echo file_get_contents( $tmp );
		exit;
	}
	
	$sql = "SELECT cat_id, cat_title 
			FROM category 
			WHERE parent_id = '{$cat_id}'";
	$data = $db->getAll( $sql );
	
	$str = '';
	foreach( $data as $v ){
		$aaa = $bbb = '';	
		$aaa = '<strong class="title">'.$v['cat_title'].'</strong>';

		$sql = "SELECT p.product_name, wp.won_product_id, t.url
				FROM won_product AS wp 
				LEFT JOIN product AS p ON wp.won_product_id = p.product_id
				LEFT JOIN {$tem_product['product_review']['table']} AS t ON wp.id = t.{$tem_product['product_review']['id']}
				WHERE  wp.cat_id IN
				(
					SELECT cat_id FROM category WHERE parent_id = '{$v['cat_id']}'
				) AND wp.web_type = '{$web_type}'   ";	
		
		$d = $db->getAll( $sql );
		
		foreach( $d as $vv ){

			$url = getReplaceUrl($vv['url']);
			$bbb .= '<li><a href="'.$url.'">'.$vv['product_name'].' Reviews</a></li>';
			
		}

		$str .= $bbb ? $aaa.'<ul>'.$bbb.'</ul>' : '';
	}
	
	file_put_contents( $tmp, $str );
	echo $str;
}


//note 删除缓存
function del_cache( $db, $tem_product ){
	$file = CACHE_DIR . get('file');
	if( file_exists( $file ) ){
		unlink( $file );
	}else{
		exit('no this cache file!');
	}
}

$m = $_GET['m'];
$m( $db , $tem_product);

